package avicit.bdp.dms.prm.Integration;

import avicit.bdp.common.base.BaseDAO;
import avicit.bdp.common.service.dao.CommonTypeDao;
import avicit.bdp.common.service.dao.DataSourceDao;
import avicit.bdp.common.service.dto.CommonTypeDTO;
import avicit.bdp.common.service.dto.DataSourceDTO;
import avicit.bdp.common.utils.DateUtils;
import avicit.bdp.common.utils.SpringApplicationContext;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 数据源消息处理
 *
 * @author xugb
 * @date 2021/11/4 17:56
 */

public class DataSourceHandle extends BaseMessageHandle {
    private static final Logger log = LoggerFactory.getLogger(DataSourceHandle.class);

    private DataSourceDao dao;

    private CommonTypeDao commonTypeDao;

    public static final String DEFAULT_TYPE_ID = "402880a4763fe3770176404c90f11164";

    public DataSourceHandle() {
        dao = SpringApplicationContext.getBean(DataSourceDao.class);
        commonTypeDao = SpringApplicationContext.getBean(CommonTypeDao.class);
    }

    @Override
    public void excuteUpdate(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            DataSourceDTO dto = convertDto(map);
            dao.updateByPrimaryKey(dto);
        }
    }

    @Override
    public void excuteInsert(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            DataSourceDTO dto = convertDto(map);
            DataSourceDTO oldDto = dao.selectByPrimaryKey(dto.getId());
            if (oldDto != null) {
                dao.updateByPrimaryKey(dto);
            } else {
                dao.insert(dto);
            }

        }
    }

    private DataSourceDTO convertDto(Map<String, Object> map) {
        String id = MapUtils.getString(map, "id");
        String name = MapUtils.getString(map, "name");
        String type = MapUtils.getString(map, "type");
        String typeId = MapUtils.getString(map, "type_id");
        if(StringUtils.isBlank(typeId)){
            CommonTypeDTO commonTypeDTO = commonTypeDao.selectByPrimaryKey(DEFAULT_TYPE_ID);
            if(commonTypeDTO == null){
                this.insertDefaultType();
            }
            typeId = DEFAULT_TYPE_ID;
        }else {
            CommonTypeDTO commonTypeDTO = commonTypeDao.selectByPrimaryKey(typeId);
            if(commonTypeDTO == null || "0".equals(typeId)){
                CommonTypeDTO commonType = commonTypeDao.selectByPrimaryKey(DEFAULT_TYPE_ID);
                if(commonType == null){
                    this.insertDefaultType();
                }
                typeId = DEFAULT_TYPE_ID;
            }
        }
        String connectionParams = MapUtils.getString(map, "connection_params");
        Integer structure = MapUtils.getInteger(map, "structure");
        Integer projectType = MapUtils.getInteger(map, "project_type");
        String remark = MapUtils.getString(map, "remark");
        String createdBy = MapUtils.getString(map, "created_by");
        if(StringUtils.isBlank(createdBy)) {
            createdBy = "1";
        }
        String creationDate = MapUtils.getString(map, "creation_date");
        String lastUpdatedBy = MapUtils.getString(map, "last_updated_by");
        String lastUpdateDate = MapUtils.getString(map, "last_update_date");
        DataSourceDTO dto = new DataSourceDTO();
        dto.setId(id);
        dto.setDatasourceName(name);
        dto.setDatasourceType(type);
        dto.setTypeId(typeId);
        dto.setConnectionParams(connectionParams);
        dto.setDatasourceStructure(structure);
        dto.setProjectType(projectType);
        dto.setRemark(remark);
        dto.setCreatedBy(createdBy);
        dto.setCreationDate(DateUtils.stringToDate(creationDate));
        dto.setLastUpdatedBy(lastUpdatedBy);
        dto.setLastUpdateDate(DateUtils.stringToDate(lastUpdateDate));
        return dto;
    }

    private void insertDefaultType(){
        CommonTypeDTO commonTypeDTO = new CommonTypeDTO();
        commonTypeDTO.setId(DEFAULT_TYPE_ID);
        commonTypeDTO.setParentId("0");
        commonTypeDTO.setIcon("database");
        commonTypeDTO.setUnfolded(0);
        commonTypeDTO.setLeaf(1);
        commonTypeDTO.setOrderBy(99);
        commonTypeDTO.setName("默认分类");
        commonTypeDTO.setType("dataSourceType");
        commonTypeDao.insert(commonTypeDTO);
    }

    @Override
    public BaseDAO getDAO() {
        return this.dao;
    }


    @Override
    public String getType() {
        return "dataSource";
    }

}
